<?php
namespace app\web\controller;

use app\model\Util; 
use app\model\Koi as Koi_mod;
use app\model\Theme; 
use app\model\Game; 

class Koi extends Base
{
    public function Koi()
    {
/**
 * Created by 五锦拼霸王
 * Desc:五锦拼霸王
 * User:cbw
 * Date: 2021/1/5
 */

global $_G,$_P;
$params = $_P;
$do = 'koi';
$ac = !empty($params['op'])?$params['op']:'list';
$themeid = isset($params['themeid'])?$params['themeid']:'';//活动id
if($themeid <= 0){
    $themeid = Theme::getDefaultThemenew();
}
$themelist = Game::getThemeListByType('five_koi',0);
$themelist = $themelist['type_theme_list'];
if(count($themelist)==1){
    if ($themeid != $themelist[0]['id']){
        $url =   $_G['siteroot'].createWebUrl($do,array("themeid"=>$themelist[0]['id'],'op'=>$ac));
        echo "<script language='javascript' type='text/javascript'>";
        echo "window.location.href='$url';";
        echo "</script>";
        exit;
    }
}

$theme = Theme::getThemeById($themeid);
if (!$theme){
    $response = modelShow(40002);
    return_msg($response, '', 'ajax');
}

$config =   Theme::getConfig($themeid);
$system= $_G['system'];
$page_index = max(1,intval($params['page']));
$page_size = 10;
$page_start = ($page_index-1)*$page_size;
$response = modelShow(40000);

if (empty($_P['createtime']['start'])){
    $start_day =db_fetchcolumn ( 'SELECT min(addtime) FROM ' . tablename ( t_fans ) );
    $start_day =  date('Y-m-d',$start_day);
}else{
    $start_day =  $_P['createtime']['start'];
}
$end_day = empty($_P['createtime']['end']) ? date('Y-m-d',TIMESTAMP): $_P['createtime']['end'] ;


//锦鲤列表
if ($ac=='list'){
    $page_total = Koi_mod::getKoiListCount($themeid);
    $list = Koi_mod::getKoiList($themeid,'',0,$page_start,$page_size);
    $show_once_add = count($list)<1?true:false;
    $return_clicent = get_defined_vars();
return view('/koi_list', $return_clicent);
}elseif ($ac=='list_add'){
    $return_clicent = get_defined_vars();
return view('/koi_list_edit', $return_clicent);
}elseif($ac=='list_once_add'){
    $insertSql = "DROP TABLE IF EXISTS `yx_".MODULE_NAME."_koi_list`;
                    CREATE TABLE `yx_".MODULE_NAME."_koi_list` (
                  `l_id` int(11) NOT NULL AUTO_INCREMENT,
                  `title` varchar(64) DEFAULT NULL COMMENT '锦鲤名称',
                  `img` varchar(128) DEFAULT NULL COMMENT '锦鲤图片',
                  `in_prize` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否在奖项中1是 2不是',
                  `is_max` tinyint(1) NOT NULL DEFAULT '2' COMMENT '是否最大兑换奖项1是 2不是',
                  `themeid` int(11) DEFAULT NULL COMMENT '活动id',
                  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '更新时间',
                  PRIMARY KEY (`l_id`)
                ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='锦鲤列表';
                INSERT INTO `yx_".MODULE_NAME."_koi_list` VALUES (1, '金锦鲤', 'http://img2020.yixuansoft.cn/images/2/2021/01/IgN8eSsdxz4oTGov1mGgMVkeEYCo3T.jpeg', 2, 2, ".$themeid.", '2021-03-21 13:05:11', '2021-01-21 13:05:11');
                INSERT INTO `yx_".MODULE_NAME."_koi_list` VALUES (2, '玉锦鲤', 'http://img2020.yixuansoft.cn/images/2/2021/01/IgN8eSsdxz4oTGov1mGgMVkeEYCo3T.jpeg', 2, 2, ".$themeid.", '2021-03-21 13:05:26', '2021-01-21 13:05:26');
                INSERT INTO `yx_".MODULE_NAME."_koi_list` VALUES (3, '银锦鲤', 'http://img2020.yixuansoft.cn/images/2/2021/01/IgN8eSsdxz4oTGov1mGgMVkeEYCo3T.jpeg', 2, 2, ".$themeid.", '2021-03-21 13:05:43', '2021-01-21 13:05:43');
                INSERT INTO `yx_".MODULE_NAME."_koi_list` VALUES (4, '铜锦鲤', 'http://img2020.yixuansoft.cn/images/2/2021/01/IgN8eSsdxz4oTGov1mGgMVkeEYCo3T.jpeg', 2, 2, ".$themeid.", '2021-03-21 13:05:58', '2021-01-21 13:05:58');
                INSERT INTO `yx_".MODULE_NAME."_koi_list` VALUES (5, '铁锦鲤', 'http://img2020.yixuansoft.cn/images/2/2021/01/IgN8eSsdxz4oTGov1mGgMVkeEYCo3T.jpeg', 2, 2, ".$themeid.", '2021-03-21 13:06:11', '2021-01-21 13:06:11');
                INSERT INTO `yx_".MODULE_NAME."_koi_list` VALUES (6, '霸王锦鲤', 'http://img2020.yixuansoft.cn/images/2/2021/01/IgN8eSsdxz4oTGov1mGgMVkeEYCo3T.jpeg', 1, 1, ".$themeid.", '2021-03-21 13:06:22', '2021-01-21 13:06:22');
                ";
    $res = db_run($insertSql);
    $response = modelShow(200);
    return_msg($response, '', 'ajax');
}elseif ($ac=='list_edit'){
    $l_id = $params['l_id'];
    $item = Koi_mod::getKoiItemById($l_id,$themeid);
    $return_clicent = get_defined_vars();
return view('/koi_list_edit', $return_clicent);
}elseif ($ac=='list_update'){
    $l_id = $params['l_id'];
    $title = trim($params['title']);
    $in_prize = $params['in_prize'];
    $is_max = $params['is_max'];
    $img = $params['img'];
    $data = [
        'title'=>$title,
        'in_prize'=>$in_prize,
        'is_max'=>$is_max,
        'img'=>tomedia(str_replace('/99.jpg','/logo.jpg',$img)),
        'themeid'=>$themeid
    ];
    if ($in_prize==2){
        $in_prize_count = Koi_mod::getKoiListCount($themeid,'',2);
        if ($in_prize_count>=5){
            $response = modelShow(30000,'抽奖锦鲤类型 锦鲤数最多为5个');
            return_msg($response, '', 'ajax');
            return;
        }
    }
    if (!$l_id){
        $res = Koi_mod::insertKoiItem($data);//新增
    }else{
        if ($is_max==1){
            $where1 = ['themeid'=>$themeid];//更新
            $data1 = ['is_max'=>2];
            $res = Koi_mod::editKoiItemByWhere($where1,$data1);
        }
        $where = ['l_id'=>$l_id];//更新
        $res = Koi_mod::editKoiItemByWhere($where,$data);
    }
    if (!$res){
        $response = modelShow(30000);
    }else{
        $response = modelShow(200);
    }
    return_msg($response, '', 'ajax');
}elseif ($ac=='list_del'){
    $l_id = $params['l_id'];
    if ($l_id>7){
        $res = Koi_mod::delKoiItemById($l_id,$themeid);
        if (!$res){
            $response = modelShow(30000);
        }else{
            $response = modelShow(200);
        }
        return_msg($response, '', 'ajax');
    }
}
//锦鲤攻略列表
if ($ac=='method_list'){
    $lottery_koi = Koi_mod::getKoiList($themeid,'',2,0,0);
    $compose_koi = Koi_mod::getKoiList($themeid,'',1,0,0);
    $page_total = Koi_mod::getKoiMethodCount($themeid);
    $page_total = (int)$page_total;
    $list = Koi_mod::getKoiMethodList($themeid,$page_start,$page_size,0);
    $show_once_add = $page_total<1?true:false;
    $return_clicent = get_defined_vars();
return view('/koi_method', $return_clicent);
}elseif ($ac=='method_add'){
    $lottery_koi = Koi_mod::getKoiList($themeid,'',2,0,0);
    $compose_koi = Koi_mod::getKoiList($themeid,'',1,0,0);
    $return_clicent = get_defined_vars();
return view('/koi_method_edit', $return_clicent);
}elseif($ac=='method_once_add'){
    $insertSql = "DROP TABLE IF EXISTS `yx_".MODULE_NAME."_koi_method`;
                    CREATE TABLE `yx_".MODULE_NAME."_koi_method` (
                      `m_id` int(11) NOT NULL AUTO_INCREMENT,
                      `first` varchar(64) DEFAULT NULL COMMENT '第1锦鲤卡',
                      `second` varchar(64) DEFAULT NULL COMMENT '第2锦鲤卡',
                      `third` varchar(64) DEFAULT NULL COMMENT '第3锦鲤卡',
                      `four` varchar(64) DEFAULT NULL COMMENT '第4锦鲤卡',
                      `five` varchar(64) DEFAULT NULL COMMENT '第5锦鲤卡',
                      `compose` varchar(64) DEFAULT NULL COMMENT '合成',
                      `status` tinyint(1) NOT NULL DEFAULT '2' COMMENT '状态 1启用 2未启用',
                      `themeid` int(11) DEFAULT NULL COMMENT '活动id',
                      `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                      `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '更新时间',
                      PRIMARY KEY (`m_id`)
                    ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COMMENT='锦鲤攻略';
                
                
                INSERT INTO `yx_".MODULE_NAME."_koi_method` VALUES (1, '5', '4', '3', '2', '1', '6', 1, ".$themeid.", '2021-03-21 13:07:20', '2021-01-21 13:07:20');
                INSERT INTO `yx_".MODULE_NAME."_koi_method` VALUES (2, '3', '3', '3', '1', '1', '6', 1, ".$themeid.", '2021-03-21 13:07:42', '2021-01-21 13:07:42');
                INSERT INTO `yx_".MODULE_NAME."_koi_method` VALUES (3, '3', '3', '2', '2', '1', '6', 1, ".$themeid.", '2021-03-21 13:08:43', '2021-01-21 13:08:43');
                INSERT INTO `yx_".MODULE_NAME."_koi_method` VALUES (4, '2', '2', '3', '4', '5', '6', 1, ".$themeid.", '2021-03-21 13:09:09', '2021-01-21 13:09:09');
                ";
    $res = db_run($insertSql);
    $response = modelShow(200);
    return_msg($response, '', 'ajax');
}elseif ($ac=='method_edit'){
    $m_id = $params['m_id'];
    $item = Koi_mod::getKoiMethodItemById($m_id,$themeid);
    $lottery_koi = Koi_mod::getKoiList($themeid,'',2,0,0);
    $compose_koi = Koi_mod::getKoiList($themeid,'',1,0,0);
    $return_clicent = get_defined_vars();
return view('/koi_method_edit', $return_clicent);
}elseif ($ac=='method_update'){
    $m_id = $params['m_id'];
    $first = $params['first'];
    $second = $params['second'];
    $third = $params['third'];
    $four = $params['four'];
    $five = $params['five'];
    $compose = $params['compose'];
    $status = $params['status'];
    $data = [
        'first'=>$first,
        'second'=>$second,
        'third'=>$third,
        'four'=>$four,
        'five'=>$five,
        'compose'=>$compose,
        'status'=>$status,
        'themeid'=>$themeid
    ];
    if (!$m_id){
        $res = Koi_mod::insertKoiMethodItem($data);
    }else{
        $where = ['m_id'=>$m_id];
        $res = Koi_mod::editKoiMethodItemById($where,$data);
    }
    if (!$res){
        $response = modelShow(30000);
    }else{
        $response = modelShow(200);
    }
    return_msg($response, '', 'ajax');
}
//奖项列表
if ($ac=='prize_list'){
    $page_total = Koi_mod::getKoiPrizeCount($themeid);
    $lottery_koi = Koi_mod::getKoiList($themeid,'',2,0,0);
    $list = Koi_mod::getKoiPrizeList($themeid,0,0,$page_start,$page_size,0);
    foreach ($list as $k => $val) {
        $list[$k]['can_del'] = true;
        $has = Koi_mod::checkKoiPrizeUsed($val['themeid'],$val['p_id']);
        if ($has){
            $list[$k]['can_del'] = false;
        }
    }


    if(checksubmit('export_submit', true)){
        $count = $page_total;
        $page_limit = 2000;

        $pagesize = ceil($count/$page_limit);
        $ex_page = max(intval($_P['ex_page']),1);
        $page_start = ($ex_page - 1) * $page_limit;
        $header = array(
            'name' => '奖品简称',  'short_name' => '奖品描述',
            'qty' => '奖品库存','price' => '奖品价值','pr'=>'中奖概率', 'show_sort' => '展示排序'
        );

        $keys = array_keys($header);
        $html = "\xEF\xBB\xBF";
        foreach ($header as $li) {
            $html .= $li . "\t ,";
        }
        $html .= "\n";
        echo $pagesize;
//        for ($j = 1; $j <= $pagesize; $j++) {


            $excel_list = Koi_mod::getKoiPrizeList($themeid,'',0,$page_start,$page_limit,0);
            if (!empty($excel_list)) {
                $size = ceil(count($excel_list) / 500);
                for ($i = 0; $i < $size; $i++) {
                    $buffer = array_slice($excel_list, $i * 500, 500);
                    foreach ($buffer as $row) {
                        $row['pr'] = $row['pr']/100;
                        foreach ($keys as $key) {
                            $data[] = $row[$key];
                        }
                        $user[] = implode("\t ,", $data) . "\t ,";
                        unset($data);
                    }
                    $html .= implode("\n", $user)."\n";
                    unset($user);
                }
            }
        //}
        //新版本导出合并
        $is_end = 0;
        if($ex_page>=$pagesize){
            $is_end = 1;
        }

        Util::WxJump($system,$html,'奖品信息',$themeid,$is_end,$pagesize);
    }


    $return_clicent = get_defined_vars();
return view('/koi_prize', $return_clicent);
}elseif ($ac=='prize_add'){
    $shangjia = [];
    if ($system['is_multiple_merchants']){
        $shangjia = Koi_mod::getShangJiaAll($themeid);
    }
    $lottery_koi = Koi_mod::getKoiList($themeid,'',2,0,0);
    $return_clicent = get_defined_vars();
return view('/koi_prize_edit', $return_clicent);
}elseif ($ac=='prize_edit'){
    $shangjia = [];
    if ($system['is_multiple_merchants']){
        $shangjia = Koi_mod::getShangJiaAll($themeid);
    }
    $p_id = $params['p_id'];
    $item = Koi_mod::getKoiPrizeItemById($themeid,$p_id);
    $lottery_koi = Koi_mod::getKoiList($themeid,'',2,0,0);
    $return_clicent = get_defined_vars();
return view('/koi_prize_edit', $return_clicent);
}elseif ($ac=='prize_del'){
    $p_id = $params['p_id'];
    $del_item = Koi_mod::delKoiPrizeItemById($themeid,$p_id);
    if (!$del_item){
        $response = modelShow(30000);
    }else{
        $response = modelShow(200);
    }
    return_msg($response, '', 'ajax');
}elseif ($ac=='prize_update'){
    $sj_id = $params['sj_id'];
    $p_id = $params['p_id'];
    $name = $params['name'];
    $short_name = $params['short_name'];//描述
    $qty = $params['qty'];
    $price = $params['price'];
    $koi_id = $params['koi_id'];
    $koi_id = $params['koi_id'];
    $koi_item = Koi_mod::getKoiItemById($koi_id,$themeid);
    $koi_title = $koi_item['title'];
    $is_lose = $params['is_lose'];
    $pr = $params['pr']*100;
    $show_sort = $params['show_sort'];
    $small_img = $params['small_img'];
    $status = $params['status'];
    $data = [
        'name'=>$name,
        'sj_id'=>$sj_id,
        'short_name'=>$short_name,
        'qty'=>$qty,
        'price'=>$price,
        'koi_id'=>$koi_id,
        'koi_title'=>$koi_title,
        'pr'=>$pr,
        'is_lose'=>(int)$is_lose,
        'show_sort'=>$show_sort,
        'small_img'=>tomedia($small_img),
        'status'=>$status,
        'themeid'=>$themeid
    ];
    if (!$p_id){
        $res = Koi_mod::insertKoiPrizeItem($data);
    }else{
        $where = ['p_id'=>$p_id];
        $res = Koi_mod::editKoiPrizeItemByWhere($where,$data);
    }
    if (!$res){
        $response = modelShow(30000);
    }else{
        $response = modelShow(200);
    }
    return_msg($response, '', 'ajax');
}elseif($ac=='prize_all_update'){
    $old_pr = $_P['old_pr']>0?$_P['old_pr']*100:$_P['old_pr'];
    $new_pr = $_P['new_pr']>0?$_P['new_pr']*100:$_P['new_pr'];
    if (!$old_pr || !$new_pr){
        $response = modelShow(30000);
    }else{
        $where = ['themeid'=>$themeid,'pr'=>$old_pr];
        $data = ['pr'=>$new_pr];
        $res = Koi_mod::editKoiPrizeItemByWhere($where,$data);
        if (!$res){
            $response = modelShow(30000);
        }else{
            $response = modelShow(200);
        }
    }

    return_msg($response, '', 'ajax');
}elseif ($ac=="prize_excel_insert") {
    $drop_sql = "DROP TABLE IF EXISTS `yx_".MODULE_NAME."_koi_prize`;
                CREATE TABLE `yx_".MODULE_NAME."_koi_prize` (
                  `p_id` int(11) NOT NULL AUTO_INCREMENT,
                  `name` varchar(128) NOT NULL DEFAULT '' COMMENT '奖项名称',
                  `short_name` varchar(128) NOT NULL DEFAULT '' COMMENT '奖项简称',
                  `qty` int(6) NOT NULL DEFAULT '1' COMMENT '奖项库存',
                  `price` int(11) DEFAULT '0' COMMENT '奖品金额',
                  `koi_id` int(11) DEFAULT NULL COMMENT '归属锦鲤id',
                  `koi_title` varchar(128) NOT NULL DEFAULT '' COMMENT '归属锦鲤名称',
                  `pr` int(4) NOT NULL DEFAULT '1' COMMENT '中奖概率',
                  `show_sort` int(2) DEFAULT '0' COMMENT '展示排序',
                  `small_img` varchar(128) NOT NULL DEFAULT '' COMMENT '奖品小图',
                  `detail_imgs` varchar(1024) NOT NULL DEFAULT '' COMMENT '奖品详情图',
                  `status` tinyint(1) NOT NULL DEFAULT '2' COMMENT '状态 1上架 2下架 3库存不足',
                  `is_lose` tinyint(1) NOT NULL DEFAULT '2' COMMENT '是否为未中奖奖项  1 是  2不是',
                  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '更新时间',
                  `themeid` varchar(11) NOT NULL DEFAULT '' COMMENT '活动id',
                  PRIMARY KEY (`p_id`)
                ) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8 COMMENT='锦鲤奖项表';";
    $sql_res = db_run($drop_sql);
    $all_arr = [];
    $file = $_FILES['file'];
    $filename = $file['tmp_name'];
    if (empty($filename)){
        $response = ['code'=>30000,'msg'=>'请选择要导入的CSV文件！','data'=>[]];
        return_msg($response, '', 'ajax');
    }
    if (!$file['size']){
        $response = ['code'=>30000,'msg'=>'文件不能为空！','data'=>[]];
        return_msg($response, '', 'ajax');
    }
    if ($file['name'] && $file['error']==0){
        $type = substr($file['name'],strpos($file['name'],"."));
        $type = strtolower($type);
        if (!in_array($type,['.csv'])){
            $response = ['code'=>30000,'msg'=>'文件类型错误！','data'=>[]];
            return_msg($response, '', 'ajax');
        }
    }
    $handle = fopen($file['tmp_name'],'r');
        $out = array ();
        $n = 0;
        while ($data = fgetcsv($handle, 10000))
        {
            $num = count($data);
            for ($i = 0; $i < $num; $i++)
            {
                $out[$n][$i] = $data[$i];
            }
            $n++;
        }
    $result = $out;
    $len_result = count($result);
    if($len_result==0){
        $response = ['code'=>30000,'msg'=>'无数据！','data'=>[]];
        return_msg($response, '', 'ajax');
    }
    $data_values = '';
    for($i = 1; $i < $len_result; $i++) //循环获取各字段值
    {
        $name = trim($result[$i][0]);
        $short_name = trim($result[$i][1]);
        $qty = trim($result[$i][2]);
        $price = trim($result[$i][3]);
        $pr = trim($result[$i][4]);
        $show_sort = trim($result[$i][5]);
        $data_values .= "('$name','$short_name','$qty','$price','$pr','$show_sort','$themeid'),";
    }
    $data_values = substr($data_values,0,-1); //去掉最后一个逗号
    fclose($handle); //关闭指针
    $sql = "INSERT INTO ".tablename(koi_prize)." (`name`, `short_name`, `qty`,`price`,`pr`,`show_sort`,`themeid`) VALUES $data_values";//sql语句
    $res = db_query($sql);
    $response = ['code'=>200,'msg'=>'操作成功,请稍后查看！','data'=>[]];
    return_msg($response, '', 'ajax');

}
//抽奖机会
if ($ac=='chance_list'){
    $page_total = Koi_mod::getKoiChanceCount($themeid,0,'');
    $list = Koi_mod::getKoiChanceList($themeid,0,'',$page_start,$page_size);
    $return_clicent = get_defined_vars();
return view('/koi_chance', $return_clicent);
}elseif ($ac=='chance_search'){
    $search_data = isset($params['search_data'])?$params['search_data']:'';
    $nickname = $search_data;
    $mobile = $search_data;

    $search = '/^0?1[3|4|5|6|7|8][0-9]\d{8}$/';
    if (preg_match( $search, $search_data )){
        $nickname = '';
    }elseif (is_string($search_data)){
        $mobile = '';
    }
    if (!empty($nickname)){
        $nickname = '%'.$nickname.'%';
    }
    $start_day1 = $start_day;
    $end_day1 = $end_day;
    if ($start_day1==$end_day1){
        $start_day1 = $end_day1 = '';
    }
    $list = Koi_mod::getKoiChanceList($themeid,0,'',$page_start,$page_size,$nickname,$mobile,$start_day1,$end_day1);
    $page_total = count($list);
    $return_clicent = get_defined_vars();
return view('/koi_chance', $return_clicent);
}
//中奖记录
if ($ac=='default_flag_list'){
    $type_id = 0;
    $type = -1;
    $status = 0;
    $koi_type_list = Koi_mod::getKoiTypeList($themeid);
    $type_list = Koi_mod::getKoiFlagTypeList($themeid);
    $prize_list = Koi_mod::getKoiPrizeTypeList($themeid);
    $total = Koi_mod::getKoiUserFlagList($themeid);
    $list = Koi_mod::getKoiUserFlagList($themeid,'default',$status,'',0,0,$page_start,$page_size);
    $page_total = count($total);
    $pager = pagination($page_total,$page_index,$page_size);
//    $type = $ac;
    $return_clicent = get_defined_vars();
return view('/koi_flag', $return_clicent);
}elseif ($ac=='koi_flag_list'){
    $status = 0;
    $type_list = Koi_mod::getKoiFlagTypeList($themeid,'koi');
    $type = -1;
    $total = Koi_mod::getKoiUserFlagList($themeid,'koi',$status);
    $list = Koi_mod::getKoiUserFlagList($themeid,'koi',$status,'',0,0,$page_start,$page_size);
    $page_total = count($total);
    $pager = pagination($page_total,$page_index,$page_size);

    $return_clicent = get_defined_vars();
return view('/koi_flag', $return_clicent);
}elseif ($ac=='flag_search'){
    $koi_type_list = Koi_mod::getKoiTypeList($themeid);
    $type_list = Koi_mod::getKoiFlagTypeList($themeid);
    $prize_list = Koi_mod::getKoiPrizeTypeList($themeid);

    $search_data = isset($params['search_data'])?$params['search_data']:'';
    if ($search_data){
        $search_data = '%'.$search_data.'%';
    }
    $status = isset($_P['status'])?(int)$_P['status']:'';

    $type = $params['type'];
    $from_type =$type;
    $type_id = isset($_P['type_id'])?(int)$_P['type_id']:'';
    $uid = isset($_P['uid'])?(int)$_P['uid']:'';
    $lists = Koi_mod::getKoiUserFlagList($themeid,$from_type,$status,$search_data,$start_day,$end_day,0,0,$type_id,$uid);
    $total = count($lists);
    if(checksubmit('export_submit', true)){

        $count = $total;
        $pagesize = ceil($count/5000);

        if ($from_type=='koi'){
            $header = array(
                'uid' => '会员UID',  'mobile' => '手机',
                'nickname' => '微信昵称','title' => '锦鲤名称','status'=>'状态',
                'created_at'=>'创建时间'
            );
        }else{
            $header = array(
                'uid' => '会员UID',  'mobile' => '手机',
                'nickname' => '微信昵称','name' => '礼品名称','status'=>'状态',
                'created_at'=>'创建时间'
            );

        }

        $keys = array_keys($header);
        $html = "\xEF\xBB\xBF";
        foreach ($header as $li) {
            $html .= $li . "\t ,";
        }
        $html .= "\n";
      //  for ($j = 1; $j <= $pagesize; $j++) {
           $page_limit = 2000;
           $ex_page = max(intval($_P['ex_page']),1);
            $condition1 = " ";
            $condition = " ";
            if ($type){
                if ($type!='all' && $type!='-1'){
                    $condition .= " and from_type='$type'";
                }
            }
            if ($type_id && $type_id!==-1){
                $condition .= " and a.from_type_id=$type_id";
            }
            if ($status && $status!==-1){
                $condition .= " and a.status=$status";
            }
            if ($search_data){
                $condition .= " and b.nickname like '".$search_data."'"." or b.mobile like '".$search_data."' ";
            }
            if ($start_day && $end_day && $start_day!=$end_day){
                $condition .= "  and a.created_at between '".$start_day."' and '".$end_day."'";
            }
            $condition1 = "  order by a.created_at desc limit ". ($ex_page - 1) *  $page_limit. ",$page_limit ";
            if ($type=='koi'){
                $sql = "select a.status,a.created_at,a.updated_at,a.f_id,a.from_type,a.from_type_id,b.uid,b.username,b.avatar,b.nickname,b.mobile,c.title,c.img from ".tablename(koi_flag).
                    " as a LEFT JOIN". tablename(t_fans). " as b on a.uid=b.uid and a.themeid=b.themeid ".
                    " LEFT JOIN". tablename(koi_list). " as c on a.from_type_id=c.l_id where a.themeid=$themeid".$condition.$condition1;
            }else{
                $sql = "select a.status,a.created_at,a.updated_at,a.f_id,a.from_type,a.from_type_id,b.uid,b.username,b.avatar,b.nickname,b.mobile,c.name,c.qty,c.price,c.koi_id,c.koi_title,c.small_img from ".tablename(koi_flag).
                    " as a LEFT JOIN". tablename(t_fans). " as b on a.uid=b.uid and a.themeid=b.themeid".
                    "  LEFT JOIN". tablename(koi_prize). " as c on c.p_id=a.from_type_id where a.themeid=$themeid".$condition.$condition1;
            }
            $list = db_select($sql);
            if (!empty($list)) {
                $size = ceil(count($list) / 500);
                for ($i = 0; $i < $size; $i++) {
                    $buffer = array_slice($list, $i * 500, 500);
                    foreach ($buffer as $row) {
                        switch ($row['status']){
                            case 1:
                                $row['status'] = '待兑换';
                                break;
                            case 2:
                                $row['status'] = '当前失效(已兑换其他)';
                                break;
                            case 3:
                                $row['status'] = '当前已兑换';
                                break;
                            case 4:
                                $row['status'] = '已核销';
                                break;
                            default:
                                break;
                        }
                        unset($info);
                        foreach ($keys as $key) {
                            $data[] = $row[$key];
                        }
                        $user[] = implode("\t ,", $data) . "\t ,";
                        unset($data);
                    }
                    $html .= implode("\n", $user)."\n";
                    unset($user);
                }
            }
       // }
        $is_end = 0;
        if($ex_page>=$pagesize){
            $is_end = 1;
        }
        if ($from_type=='koi'){
            //新版本导出合并
            Util::WxJump($system,$html,'用户锦鲤中奖记录',$themeid,$is_end,$pagesize);
        }else{
            //新版本导出合并
            Util::WxJump($system,$html,'用户默认中奖记录',$themeid,$is_end,$pagesize);
        }
    }
    $list = Koi_mod::getKoiUserFlagList($themeid,$from_type,$status,$search_data,$start_day,$end_day,$page_start,$page_size,$type_id,$uid);

    $page_total = $total;
    $pager = pagination($page_total,$page_index,$page_size);
    if ($type!=''){
        $status = '';
        $type_id = '';
    }
    if ($type_id!=''){
        $type = '';
        $status = '';
    }
    $return_clicent = get_defined_vars();
return view('/koi_flag', $return_clicent);
}elseif ($ac=='flag_update'){
    $f_id = $_P['f_id'];
    $themeid = $_P['themeid'];
    $where = [];
    $data = [];
    $res = Koi_mod::verificationFlag($f_id,$themeid);
    if (!$res){
        $response = modelShow(30000);
    }else{
        $response = modelShow(200);
    }
    return_msg($response, '', 'ajax');
}

//订阅消息列表
if ($ac=='sub_list'){
    $page_total = Koi_mod::getKoiSubCount($themeid,0,1);
    $list = Koi_mod::getKoiSubList($themeid,0,0,$page_start,$page_size);
    $return_clicent = get_defined_vars();
return view('/koi_msg', $return_clicent);
}elseif ($ac=='send_sub'){
    $list = Koi_mod::getKoiSubList($themeid,0,0,$page_start,200);
    $total = 0;
    foreach ($list as $k =>$v){
        $res = Koi_mod::sendWxMsg($v['uid'],$system,'pages/index/index',$themeid);
        if ($res){
            $total+=1;
        }
    }
    $msg = '已经成功更新'.$total.'条';
    $response = modelShow(200,$msg);
    return_msg($response, '', 'ajax');
}
//五锦拼霸王设置


}
}